﻿using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace DataChangeTools.WebApi.Controllers
{
    /// <summary>
    /// Controller基类
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public abstract class BasicsController : ControllerBase
    {

        //方案2：定义一个动态的存放repository的对象，每次调用接口给这个repository赋值
        /// <summary>
        /// 当前控制器操作对象
        /// </summary>
        public dynamic? thisRepostory = null;

        public object[]? methodParam;

        /// <summary>
        /// 必选参数
        /// </summary>
        public Dictionary<string, string>? mustParam = null;

        /// <summary>
        /// 可选参数
        /// </summary>
        public Dictionary<string, string>? minorParam = null;

        /// <summary>
        /// 参数汇总
        /// </summary>
        public Dictionary<bool, Dictionary<string, string>>? param = null;

        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="mustParam">必选条件</param>
        /// <param name="minorParam">可选条件</param>
        /// <returns></returns>
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual dynamic GetListByParam(Dictionary<bool, Dictionary<string, string>> param)
        {
            // 获取 MyClass 类型
            var thisRepostoryType = thisRepostory?.GetType();
            MethodInfo method = thisRepostoryType.GetMethod("GetListByParam");
            return method.Invoke(thisRepostory, new object[] { param });
        }

        /// <summary>
        /// 动态方法操作
        /// </summary>
        /// <param name="MethodName"></param>
        /// <returns></returns>
        [ApiExplorerSettings(IgnoreApi = true)]
        public dynamic Method(string? MethodName)
        {
            var thisRepostoryType = thisRepostory?.GetType();
            MethodInfo method = thisRepostoryType.GetMethod(MethodName);
            var methodResult = method.Invoke(thisRepostory, methodParam);
            methodParam = new object[] { };//清空参数
            return methodResult;
        }
    }
}
